function loadNetworkSettings(form) {
	httpGet('network/config/get', function(res) {
		if(res.code == 0) {
			var data = res.data;
			form.val("network", {
				"locip": data.ip,
				"locgetway": data.getway,
				"locmask": data.netmask,
				"locdns": data.dns
			})
		}
	})
}

function getServerConfig(form) {
	httpGet('server/config/get',null, function(res) {
		if(res.code == 0) {
			var data = res.data;
			if(data.server){
				var server = data.server.split(":");
				form.val("server", {
					"serverip": server[0],
					"serverport": server[1]
				})
			}

			if(data.room_id) {
				setTimeout(function() {
					httpPost('room/info',{room_id:data.room_id}, function(res) {
						if(res.code == 0) {
							var data = JSON.parse(res.data);
							if(data.data) {
								$('#room .layui-unselect').val(data.data)
							}
						}
					})
				}, 1000)
			}
		}
	})
}


function testServerConfig(field) {

	httpPost('server/config/test',field, function(res) {
		if(res.code == 0) {
			layer.msg('测试通过', {
				icon: 6
			});
		} else {
			layer.msg('服务器不通', {
				icon: 5
			});
		}
	})
}

function saveNetworkConfig(field) {
	httpPost('network/config/set', field, function(res) {
		if(res.code == 0) {
            layer.msg('保存成功', {
            	icon: 6
            });
			setTimeout(function() {
				window.top.location.href = "http://" + field.locip+":8080";
			}, 2000)

		} else {
			layer.msg('保存失败', {
				icon: 5
			});
		}
	})
}

function saveServerConfig(field) {
	httpPost('server/config/set',field, function(res) {
		if(res.code == 0) {
			layer.msg('保存成功', {
				icon: 6
			});
			setTimeout(function(){
				window.location.reload(); 
			},1000)
		} else {
			layer.msg('保存失败', {
				icon: 5
			});
		}
	})
}

function saveMQTTSettings(field){
	httpPost('mqtt/config/save', field, function(res) {
		if(res.code == 0) {
			layer.confirm('保存成功。重启终端才能生效，是否现在重启终端?', {
				icon: 3,
				title: '提示'
			}, function(index) {
				//do something
				httpGet('sys/reboot', function(res) {
					if(res.code == 0) {
						var index = layer.load(2, {
							time: 60 * 1000
						});
						setTimeout(function() {
							layer.close(index);
							window.top.location.reload();
						}, 20000)
					}
				});
				layer.close(index);
			});
		} else {
			layer.msg('保存失败', {
				icon: 5
			});
		}
	})
}


function updateSystem(field){
	var downloadUrl=field.downloadUrl;
	if((downloadUrl.indexOf("http")!=0)||(!downloadUrl.endsWith(".apk"))){
		layer.msg('下载地址格式错误', {
			icon: 5
		});
		return ;
	}
	httpPost('terminal/update',{downloadUrl:downloadUrl}, function(res) {
		if(res.code == 0) {
			var index = layer.load(2, {
				time: 90 * 1000
			});
			setTimeout(function() {
				layer.close(index);
				window.top.location.reload();
			}, 90000)
		}
	})
}

function bindRoom() {
	var rid = $('#pidSelect').attr('value');
	if(rid) {
		httpPost('room/bind',{room_id:rid,room_name:encodeURIComponent(roomMap[rid])}, function(res) {
			if(res.code == 0) {
				layer.confirm('保存成功。重启终端才能生效，是否现在重启终端?', {
                				icon: 3,
                				title: '提示'
                			}, function(index) {
                				//do something
                				httpGet('sys/reboot', function(res) {
                					if(res.code == 0) {

                					}
                				});

                				layer.close(index);
                			});
			} else {
				layer.msg('保存失败', {
					icon: 5
				});
			}
		})
	}
}

function changeUser(field) {
	httpPost('user/change',field, function(res) {
		if(res.code == 0) {
			layer.confirm('保存成功，重启程序生效。是否现在重启程序?', {
				icon: 3,
				title: '提示'
			}, function(index) {
				//do something
				httpGet('sys/reboot', function(res) {
					if(res.code == 0) {
						var index = layer.load(2, {
							time: 60 * 1000
						});
						setTimeout(function() {
							layer.close(index);
							window.top.location.href = "http://" + field.locip;
						}, 60000)
					}
				});

				layer.close(index);
			});
		} else {
			layer.msg('保存失败', {
				icon: 5
			});
		}
	})
}


function  saveSerialportSettings(field){
	httpPost('serialport/info/save',field, function(res) {
		if(res.code == 0) {
			layer.confirm('保存成功，重启程序生效。是否现在重启程序?', {
				icon: 3,
				title: '提示'
			}, function(index) {
				//do something
				httpGet('sys/reboot', function(res) {
					if(res.code == 0) {
						var index = layer.load(2, {
							time: 5000
						});
						setTimeout(function() {
							layer.close(index);
							window.top.location.reload();
						}, 5000)
					}
				});

				layer.close(index);
			});
		} else {
			layer.msg('保存失败', {
				icon: 5
			});
		}
	})
}


function loadSerialportSettings(form){
	httpGet('serialport/info/load', function(res) {
		if(res.code == 0) {
			var data = res.data;
			form.val("serialport",data)
		}
	});
}
